from rest_framework import serializers
from users.models import User
from questions.models import Label, Question, Reply
from headlines.models import Article
from recruit.models import Enterprise


# 用于回复问题的用户信息的序列化器类
class UserSerializer1(serializers.ModelSerializer):
    """用于回复问题的用户信息的序列化器类"""

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')


# 上一级评论序列器
class SubsReplySerializer(serializers.ModelSerializer):
    """父评论的序列化器类"""
    user = UserSerializer1(read_only=True)

    class Meta:
        model = Reply
        fields = ('id', 'content', 'createtime', 'useful_count', 'unuseful_count', 'user')


# Label
class LabelModelSerializer(serializers.ModelSerializer):
    """
    个人中心标签序列化器
    """

    class Meta:
        model = Label
        fields = ('id',)


class QuestionModelSerialzier(serializers.ModelSerializer):
    """
    个人中心问题序列化器
    """

    class Meta:
        model = Question
        fields = ('id', 'createtime', 'labels', 'reply',
                  'replyname', 'replytime', 'title',
                  'unuseful_count', 'useful_count', 'user', 'visits')


class AnswerQuestionSerializer(serializers.ModelSerializer):
    # 父评论的序列化器类
    subs = SubsReplySerializer(many=True, read_only=True)

    # 用于回复问题的用户信息的序列化器类
    user = UserSerializer1(read_only=True)

    class Mete:
        model = Reply
        fields = ('id', 'content', 'createtime', 'useful_count',
                  'problem', 'unuseful_count', 'subs', 'user', 'parent')


class ArticleSerializer(serializers.ModelSerializer):
    """文章的序列化器类"""

    class Meta:
        model = Article
        fields = ('id', 'title')


class UserSerializer_Two(serializers.ModelSerializer):
    # 文章的序列化器类
    articles = ArticleSerializer(many=True)
    """用户的序列化器类"""

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'fans')


# 收藏的文章序列化器类
class CollecedArticleSerializer(serializers.ModelSerializer):
    """收藏的文章序列化器类"""
    collected = serializers.BooleanField()
    # 用户的序列化器类
    user = UserSerializer_Two()

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime',
                  'collected_users', 'image', 'visits')


# 关注的企业序列化器类
class EnterpisesSerializer(serializers.ModelSerializer):
    """关注的企业序列化器类"""
    recruits = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Enterprise
        fields = ('id', 'name', 'labels', 'logo', 'summary', 'recruits')


# 更新数据  接口
class UpdateMyfileSerializer(serializers.ModelSerializer):
    """个人信息序列化器类"""

    # 个人中心标签序列化器
    labels = LabelModelSerializer(many=True, read_only=True)
    # 个人中心标签序列化器
    questions = QuestionModelSerialzier(many=True, read_only=True)
    # 父评论的序列化器类
    answer_question = AnswerQuestionSerializer(many=True, read_only=True)
    # 收藏的文章序列化器类
    collected_articles = CollecedArticleSerializer(many=True, read_only=True)
    # 关注的企业序列化器类
    enterpises = EnterpisesSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'realname', 'birthday',
                  'sex', 'avatar', 'website', 'email', 'city', 'address',
                  'labels', 'questions', 'answer_question',
                  'collected_articles', 'enterpises')
        extra_kwargs = {
            'id': {
                'read_only': True
            },
            'username': {
                'read_only': True
            }
        }


# 个人信息序列化器类
class UserInfoModelSerialzier(serializers.ModelSerializer):
    """个人信息序列化器类"""

    # 个人中心标签序列化器
    labels = LabelModelSerializer(many=True, read_only=True)
    # 个人中心标签序列化器
    questions = QuestionModelSerialzier(many=True, read_only=True)
    # 父评论的序列化器类
    answer_question = AnswerQuestionSerializer(many=True, read_only=True)
    # 收藏的文章序列化器类
    collected_articles = CollecedArticleSerializer(many=True, read_only=True)
    # 关注的企业序列化器类
    enterpises = EnterpisesSerializer(many=True, read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'realname', 'birthday',
                  'sex', 'avatar', 'website', 'email', 'city', 'address',
                  'labels', 'questions', 'answer_question',
                  'collected_articles', 'enterpises')
        extra_kwargs = {
            'id': {
                'read_only': True
            },
            'username': {
                'read_only': True
            },
            'mobile': {
                'read_only': True
            },
            'realname': {
                'read_only': True
            },
            'birthday': {
                'read_only': True
            },
            'sex': {
                'read_only': True
            },
            'avatar': {
                'read_only': True
            },
            'website': {
                'read_only': True
            },
            'email': {
                'read_only': True
            },
            'city': {
                'read_only': True
            },
            'address': {
                'read_only': True
            }
        }


# 修改
class UserPasswordModelSerializer(serializers.ModelSerializer):
    """修改密码的序列化器类"""

    # token = serializers.CharField(label='JWT Token', write_only=True)

    class Meta:
        model = User
        fields = ('password',)

        extra_kwargs = {
            'password': {
                'write_only': True
            }
        }

    def update(self, instance, validated_data):
        """修改的密码进行加密再返回"""

        password = validated_data.pop('password', None)

        if password:
            instance.set_password(password)
            instance.save()

        return instance
